package com.midou.administration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.inject.internal.cglib.core.$CollectionUtils;
import com.midou.administration.bo.CommonResult;
import com.midou.administration.bo.OrderInfoBo;
import com.midou.administration.bo.UserBo;
import com.midou.administration.entity.*;
import com.midou.administration.mapper.*;
import com.midou.administration.mapper.OrderMapper;
import com.midou.administration.service.OrderInfoService;
import com.midou.administration.utils.Toot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MrChen
 * @since 2021-03-24
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private UsersMapper usersMapper;

    //餐桌接口
    @Autowired
    private DeskMapper deskMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private BusinessMapper businessMapper;



    public List<OrderInfo> orderInfoQuery(Integer id){
        List<OrderInfo> orderList=orderInfoMapper.orderInfoQuery(id);
        return orderList;
    }

    //查询会员手机号是否存在
    public CommonResult queryphone(String phone){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_phone",phone);
        Users users=usersMapper.selectOne(queryWrapper);
        if(users==null){
            return new CommonResult(500,"会员不存在");
        }else {
            return new CommonResult(200,null);
        }
    }
    //现金支付修改订单状态
    public CommonResult xianjinZhiFu( UserBo userBo){
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("order_number",userBo.getOrderNumber());
        Order order=orderMapper.selectOne(queryWrapper1);
        order.setOrderState(1);
        order.setOrderPaymentMethod(1);
        order.setOrderPrice(userBo.getMoney());
        Desk desk = this.deskMapper.selectById(order.getDeskId());
        desk.setDeskStatus(3);
        this.deskMapper.updateById(desk);
        QueryWrapper queryWrapper2=new QueryWrapper();
        queryWrapper2.eq("bu_id",userBo.getUserBusinessId());
        Business busines=businessMapper.selectOne(queryWrapper2);
        busines.setBuProfit(busines.getBuProfit()+userBo.getMoney());
        businessMapper.updateById(busines);
        int num=orderMapper.updateById(order);
        if(num>0){
            return new CommonResult(200,"支付完成");
        }else {
            return new CommonResult(500,"支付失败");
        }
    }
    //打印小票前查询
    public String daYinQuery(OrderInfoBo orderInfoBo) {

        List<OrderInfo> list = orderInfoMapper.orderInfoQuery(orderInfoBo.getOrdId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String url = simpleDateFormat.format(orderInfoBo.getRiqi());
        //写入
        String txtName ="D:\\" +orderInfoBo.getName() + "" + orderInfoBo.getDingdanhao()+ ".txt";
        File file = new File(txtName);
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            File file1 = new File(txtName);
            if(!file1.exists()){
                fw = new FileWriter(file, true);
            }else {
                fw = new FileWriter(file, false);
            }

            bw = new BufferedWriter(fw);
            bw.write("\n" + orderInfoBo.getName() + "-------" + "打印小票");
            bw.write("\n桌号:" + orderInfoBo.getZhuohao() + "\t\t\t日期:" + url);
            bw.write("\n员工:" + orderInfoBo.getYuangong() + "\t\t\t单号:" + orderInfoBo.getDingdanhao());
            bw.write("\n---------------------------------------------------------------------");
            bw.write("\n菜品名称" + "\t\t数量" + "\t单价" + "\t金额");
            for (OrderInfo orderInfo : list) {
                bw.write("\n" + orderInfo.getMenu().getMenuName() + "\t\t" + orderInfo.getOiSize() + "\t" + orderInfo.getMenu().getMenuPrice() + "\t" + orderInfo.getOiSize() * orderInfo.getMenu().getMenuPrice());
            }
            bw.write("\n---------------------------------------------------------------------");
            bw.write("\n优惠:" + orderInfoBo.getYouhui());
            bw.write("\n");
            bw.write("\n原价:" + orderInfoBo.getYuanjia());
            bw.write("\n");
            bw.write("\n实际:" + orderInfoBo.getShifu());
            bw.write("\n");
            bw.write("\n备注:" + orderInfoBo.getBeizhu());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return txtName;
    }
    //打印厨房端口
    public String chufang(OrderInfoBo orderInfoBo){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("order_id",orderInfoBo.getOrdId());
        Order order=orderMapper.selectOne(queryWrapper);
        order.setOrderState(0);
        order.setClerkId(orderInfoBo.getClearId());
        orderMapper.updateById(order);
        List<OrderInfo> list = orderInfoMapper.orderInfoQuery(orderInfoBo.getOrdId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String url = simpleDateFormat.format(orderInfoBo.getRiqi());
        //写入
        String txtName ="D:\\" +orderInfoBo.getName() + "厨房端" + orderInfoBo.getDingdanhao()+ ".txt";
        File file = new File(txtName);
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            File file1 = new File(txtName);
            if(!file1.exists()){
                fw = new FileWriter(file, true);
            }else {
                fw = new FileWriter(file, false);
            }

            bw = new BufferedWriter(fw);
            bw.write("\n" + orderInfoBo.getName() + "-------" + "订单详情");
            bw.write("\n桌号:" + orderInfoBo.getZhuohao() + "\t\t\t日期:" + url);
            bw.write("\n员工:" + orderInfoBo.getYuangong() + "\t\t\t单号:" + orderInfoBo.getDingdanhao());
            bw.write("\n---------------------------------------------------------------------");
            bw.write("\n菜品名称" + "\t\t数量" + "\t单价" + "\t金额");
            for (OrderInfo orderInfo : list) {
                bw.write("\n" + orderInfo.getMenu().getMenuName() + "\t\t" + orderInfo.getOiSize() + "\t" + orderInfo.getMenu().getMenuPrice() + "\t" + orderInfo.getOiSize() * orderInfo.getMenu().getMenuPrice());
            }
            bw.write("\n---------------------------------------------------------------------");
            bw.write("\n");
            bw.write("\n备注:" + orderInfoBo.getBeizhu());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return txtName;
    }

    @Override
    public int addInfo(Toot toot) {
        //总金额
        Double sum = 0.0;

        for (Menu menu : toot.getList()) {
            OrderInfo info = new OrderInfo();
            info.setOiMenuId(menu.getMenuId());
            info.setOiOrderId(toot.getId());
            info.setOiSize(menu.getGoodsNumber());
            info.setOiDownTime(new Date());
            info.setOiState(0);
            this.orderInfoMapper.insert(info);
            //计算订单总额度
            sum += info.getOiSize() * menu.getMenuPrice();
        }
        //更新订单金额 根据id更新
        Order order = this.orderMapper.selectById(toot.getId());
        order.setOrderPrice(sum);
       int count = this.orderMapper.updateById(order);
       if(count>0){
           return 1;
        }
       return 0;
    }
}
